﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace MediaBrowser.CustomWindow
{
    public static class ExtensionMethods
    {
        private static Action EmptyDelegate = delegate() { };

        public static void Refresh(this DispatcherObject uiElement)
        {
            uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
        }
    }

    /// <summary>
    /// Fenêtre Animée
    /// </summary>
    public class AnimatedWindow : CustomWindowStyle
    {
        public AnimatedWindow()
        {
            Closing += AnimatedWindow_Closing;
        }

        /// <summary>
        /// Surcharge de la méthode ShowDialog pour déclencher l'animation 
        /// </summary>
        /// <returns></returns>
        public bool? ShowDialogAnimated()
        {
            AnimShowWindow();
            return true;
        }

        /// <summary>
        /// Animation de fin
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void AnimatedWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            AnimHideWindow();
        }

        #region Animation Windows

        #region AnimShowWindow

        /// <summary>
        /// Animation à l'ouverture
        /// </summary>
        private void AnimShowWindow()
        {
            if (this.Owner != null)
            {
                this.Owner.IsEnabled = false;
            }

            Storyboard board = new Storyboard();
            double durationoAnim = 0;

            board.AccelerationRatio = 0.5;
            board.DecelerationRatio = 0.5;
            durationoAnim = 1000;

            DoubleAnimation oAnim = new DoubleAnimation(0.5, 1, new Duration(TimeSpan.FromMilliseconds(durationoAnim)));
            Storyboard.SetTargetProperty(oAnim, new PropertyPath(Window.OpacityProperty));
            Storyboard.SetTarget(oAnim, this);

            board.Children.Add(oAnim);

            board.Begin();
            this.ShowDialog();
        }

        #endregion

        #region AnimHideWindow

        /// <summary>
        /// Animation à la fermeture
        /// </summary>
        private void AnimHideWindow()
        {
            Storyboard board = new Storyboard();

            board.AccelerationRatio = 0.5;
            board.DecelerationRatio = 0.5;

            DoubleAnimation oAnim = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromMilliseconds(500)));
            Storyboard.SetTargetProperty(oAnim, new PropertyPath(Window.OpacityProperty));
            Storyboard.SetTarget(oAnim, this);

            board.Children.Add(oAnim);

            board.Completed += board_Completed;

            board.Begin();
        }

        /// <summary>
        /// Fermeture compléte à la fin de la fenêtre
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void board_Completed(object sender, EventArgs e)
        {
            Storyboard board = (Storyboard)((ClockGroup)sender).Timeline;
            DependencyObject d = Storyboard.GetTarget(board.Children.First());
            AnimatedWindow animatedWindow = d as AnimatedWindow;

            if (animatedWindow.Owner != null)
            {
                animatedWindow.Owner.IsEnabled = true;
            }

            animatedWindow.Closing -= AnimatedWindow_Closing;
            animatedWindow.Close();
        }

        #endregion

        #endregion

    }
}
